package work.vcloud.baseinfo.controller;

import com.alibaba.fastjson.JSONObject;

import work.vcloud.baseinfo.utils.HttpUtil;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@Scope("prototype")
public class StudentController extends BaseController {

	@Resource
	OpenDao openDao;

	@Value("${api.passwordReceive}")
	private String passwordReceive;

	@Value("${auth.app_id}")
	private String appId;
	
	/**
	 * 新增学生信息
	 */
	@RequestMapping("api/student.insert")
	public OutputVo insert() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");

//			//插入用户
//			map.put("type", '2');
//			map.put("sqlMapId", "user.insert");
//			map.put("fk_user", openDao.insert(map));

			//插入学生
			map.put("sqlMapId", "student.insert");
			map.put("fk_user", openDao.insert(map));

//			//插入学生监护人信息
//			Map<String,Object> family = (Map<String,Object>)JSONObject.parseObject(map.get("family").toString());
//			family.put("fk_student", map.get("fk_user"));
//			family.put("sqlMapId", "family.insert");
//			openDao.insert(family);
			
			
			String ids = map.get("fk_user").toString();
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("ids",ids);
			jsonObject.put("type", 2);
			String accessToken = ((Map)session.getAttribute(appId)).get("access_token").toString();
			HttpUtil.doPost2(passwordReceive, jsonObject, accessToken);
			
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	/**
	 * 修改学生信息
	 */
	@RequestMapping("api/student.update")
	public OutputVo update() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
//			//修改用户
//			map.put("type", '2');
//			map.put("sqlMapId", "user.update");
//			map.put("fk_user", openDao.insert(map));
			
			//修改学生
			map.put("sqlMapId", "student.update");
			openDao.update(map);
//			//修改学生监护人信息
//			Map<String,Object> family = (Map<String,Object>)JSONObject.parseObject(map.get("family").toString());
//			family.put("fk_student", map.get("id"));
//			family.put("sqlMapId", "family.select");
//			Map<String, Object> result = (Map<String, Object>) openDao.queryForObject(family);
//			if (result == null) {
//				family.put("sqlMapId", "family.insert");
//				openDao.insert(family);
//			} else {
//				family.put("sqlMapId", "family.update");
//				openDao.update(family);
//			}
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
}
